__file__ = 'A07.py'
__author__ = 'Jerry Liu'
__date__ = '2016-04-13'

"""
    问题：有个目录，里面是你自己写过的程序，统计一下你写过多少行代码。包括空行和注释，但是要分别列出来。
"""
import re

# 行数
def getLinesCount(path):
    count = 0
    f = open(path, 'rb')
    while True:
        buffer = f.read(1024)
        if not buffer:
            break
        count += buffer.count('\r\n'.encode('utf-8'))
    f.close()
    print(count)
    return count

# 正则计算行数
def getLineCount(path):
    annotation = r'\s*[#]\w*'
    space = r'^\s+$'

    code, blank, note = 0, 0, 0
    f = open(path, 'rb')
    line = f.readline()
    line = line.decode('utf-8')

    while line:
        if re.match(space, line):
            blank += 1
            code += 1
        elif re.match(annotation, line):
            note += 1
            code += 1
            print(line)
        else:
            code += 1

        line = f.readline().decode('utf-8')

    print("共有%d行, 其中包含空行 %d行, 注释 %d行。" % (code, blank, note))

if __name__ == '__main__':
    getLineCount('d://A02.py')
